package com.doit.学;


import java.io.*;
import java.util.ArrayList;
    @SuppressWarnings("all")
    public class test04 {
        //static String name;
        public static void main(String[] args) throws IOException {
            File str = new File("D:\\move\\练习.mp4");
            File dir = new File("D:\\move\\part");
            ArrayList<File> list = new ArrayList<>();

            fenge(list,str,dir);
             hebin(list,dir);
//            add_file(dir, list);
        }


        public static void hebin(ArrayList<File> list, File dir) throws IOException {
            OutputStream out = new FileOutputStream(dir.getPath() + "\\" + "练习.mp4");
            int len = 0;
            byte[] bytes = new byte[1024 * 100];
            for (File f : list) {
                InputStream in = new FileInputStream(f);
                if (f.getName().toLowerCase().endsWith(".part")) {
                    while ((len = in.read(bytes)) != -1) {
                        out.write(bytes, 0, len);
                        out.flush();

                    }
                    in.close();
                }
            }
            out.close();

        }


        public static void fenge(ArrayList<File> list, File str, File dir) throws IOException {
            dir.mkdirs();
            InputStream in = new FileInputStream(str);
            int len = 0;
            int i = 0;
            byte[] bytes = new byte[1024 * 100];
            while ((len = in.read(bytes)) != -1) {
                String name = dir.getPath() + "\\" + (++i) + ".part";
                OutputStream out = new FileOutputStream(name);
                out.write(bytes, 0, len);
                out.flush();
                list.add(new File(name));
                out.close();
            }
            in.close();

        }

        private static void add_file(File dir, ArrayList<File> list) throws IOException {

            FileOutputStream out = new FileOutputStream(dir.getPath() + "\\" + ".mp4");
            int i = 0;
            for (File name : list) {
                FileInputStream file = new FileInputStream(name);
                if (name.getName().toLowerCase().endsWith(".part"))//如果是文件且是part结尾
                {
                    int len;
                    byte[] bytes = new byte[1024 * 100];
                    while ((len = file.read(bytes)) != -1) {
                        out.write(bytes, 0, len);
                        out.flush();
                    }
                    file.close();
                    System.out.println("正在提取第" + (++i) + "个文件");
                }
            }

        }

}
